package com.example.basestationforballonproject.utils;

//import com.example.basestationforballonproject.BaseStationService;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;

import com.example.basestationforballonproject.BaseStationService;
//import android.widget.Toast;
import com.example.basestationforballonproject.MainActivity;

public class SMSBroadcastReceiver extends BroadcastReceiver
{

	private int notifyID = 0;
	
	public SMSBroadcastReceiver() {
		super();
	}
	
	
	public void onReceive(Context cxt, Intent intent)
	{
		//		int shortToast = Toast.LENGTH_SHORT;
		if(intent.getAction().equals("SMS_SENT"))
		{
			switch(getResultCode())
			{
			case Activity.RESULT_OK:
				BaseStationService.logger.fileLog("SMS went out");
				createNotification("SMS out", "SMS went out", cxt);
				//				AlertHelper.getDialog("SMS was sent", "SMS went out - check your log for the delivery report", cxt).show();
				break;
			case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
				//				Toast.makeText(cxt, "SMS NOT SENT - Generic failure", shortToast).show();
				BaseStationService.logger.fileLog("SMS NOT SENT - Generic failure");
				createNotification("SMS NOT SENT", "SMS NOT SENT - Generic failure", cxt);
				break;
			case SmsManager.RESULT_ERROR_NO_SERVICE:
				//				Toast.makeText(cxt, "SMS NOT SENT - No service", shortToast).show();
				BaseStationService.logger.fileLog("SMS NOT SENT - No service");
				createNotification("SMS NOT SENT", "SMS NOT SENT - No Service", cxt);
				break;
			case SmsManager.RESULT_ERROR_NULL_PDU:
				//				Toast.makeText(cxt, "SMS NOT SENT - Null PDU", shortToast).show();
				BaseStationService.logger.fileLog("SMS NOT SENT - Null PDU");
				createNotification("SMS NOT SENT", "SMS NOT SENT - Null PDU", cxt);
				break;
			case SmsManager.RESULT_ERROR_RADIO_OFF:
				//				Toast.makeText(cxt, "SMS NOT SENT - Radio off", shortToast).show();
				BaseStationService.logger.fileLog("SMS NOT SENT - Radio off");
				createNotification("SMS NOT SENT", "SMS NOT SENT - Radio off", cxt);
				break;
			}
		}
		else if(intent.getAction().equals("SMS_DELIVERED"))
		{
			switch(getResultCode())
			{
			case Activity.RESULT_OK:
				//				AlertHelper.getDialog("SMS was delivered", "SMS was delivered!", cxt).show();	
				BaseStationService.logger.fileLog("Delivery Report: OK :)");
				createNotification("SMS delivered", "Delivery Report: OK :)", cxt);
				break;
			case Activity.RESULT_CANCELED:
				//				Toast.makeText(cxt, "SMS not delivered", shortToast).show();
				BaseStationService.logger.fileLog("Delivery Report: CANCELED :(");
				createNotification("SMS NOT delivered", "Delivery Report: CANCELED :(", cxt);
				break;
			}
		}
	}

	@SuppressWarnings("deprecation")
	private void createNotification(String title, String text, Context context) {
		notifyID++;

		Notification notification = new Notification(com.example.basestationforballonproject.R.drawable.ic_launcher, title, System.currentTimeMillis());
		notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
		Intent intent = new Intent(context, MainActivity.class);
		PendingIntent pd = PendingIntent.getActivity(context, 1, intent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 

		notification.setLatestEventInfo(context, title, text, pd);
		NotificationManager mN = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		mN.notify(notifyID, notification);
	}
}
